﻿using EnvDTE;
using SolidEdgePart;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TGComacAddin.View
{
    /// <summary>
    /// TGSelectedItemsDlg.xaml 的交互逻辑
    /// </summary>
    public partial class TGSelectedItemsDlg :System.Windows.Window
    {
        command.ElementsViewModel _viewModel;
        command.TGSelect _cmdSelect;
        private PartDocument _partDoc = null;
        public TGSelectedItemsDlg(command.ElementsViewModel viewModel, PartDocument partDoc, command.TGSelect cmdSelect)
        {
            InitializeComponent();
            _viewModel = viewModel;
            DataContext = _viewModel;
            _partDoc = partDoc;
            _cmdSelect = cmdSelect;
        }

        private SolidEdgeFramework.HighlightSet _hiset = null;
         public SolidEdgeFramework.HighlightSet Hilitset
        {
            get
            {
                if(null==_hiset)
                {
                    _hiset = _partDoc.HighlightSets.Add();
                    //_hiset.Color = 0x00FF0000;
                    //_hiset.Width = 2;
                }
                return _hiset;
            }
        }

        private List<command.ElementDesc> SelectedItems = new List<command.ElementDesc>();  
        private void lbSelectedItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                foreach (command.ElementDesc item in e.AddedItems)
                {
                    SelectedItems.Add(item);
                    Hilitset.AddItem(item.Geometry);
                }
                foreach (command.ElementDesc item in e.RemovedItems)
                {
                    SelectedItems.Remove(item);
                    for (int i = Hilitset.Count; i >0; i--)
                    {
                        if (Hilitset.Item(i) == item.Geometry)
                        {
                            Hilitset.RemoveItem(i);
                        }
                    }
                }
                if(SelectedItems.Count>0)
                {
                    Hilitset.Draw();
                    SolidEdgeFramework.Window win = _partDoc.Application.ActiveWindow as SolidEdgeFramework.Window;
                    win.View.Update();
                }
                else
                {
                   
                }
               
            }
            catch(Exception ex)
            {

            }

        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for(int i = SelectedItems.Count; i >0; i--)
                {
                    _viewModel.ElementDescArr.Remove(SelectedItems[i-1]);
                }
            }
            catch (Exception ex)
            {

            }
         
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            _cmdSelect.Status = (_cmdSelect.ElementIds.Count > 0)? command.SelectStatus.Success:command.SelectStatus.Cancel;
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _cmdSelect.Status = command.SelectStatus.Cancel;
            Close();
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _partDoc.Application.ActiveSelectSet.RemoveAll();
                Hilitset.RemoveAll();
                _viewModel.ElementDescArr = new ObservableCollection<command.ElementDesc>();
            }
            catch (Exception ex)
            {

            }
        }
    }
}
